package com.ruoyi.rabbitMQ;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.H5Send.demo.SmsSendDemo;
import com.ruoyi.H5Send.demo.SmsVariableSend;
import com.ruoyi.order.domain.SysOrderOne;
import com.ruoyi.packages.Utils.SurveyUtils;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @Description:
 * @Author: 吕宇宏
 * @Date: 2024/7/31 16:36
 */
@Component
public class RabbitRevice {
        @Autowired
        private RedisTemplate redisTemplate;

        @Autowired
        private SmsVariableSend smsVariableSend;

        @Autowired
        private SurveyUtils surveyUtils;

        @Autowired
        private SmsSendDemo smsSendDemo;


        //发送短信的mq
        @RabbitListener(queues = "sendSMS")
        public void sendAuthorized(String msg) {



            Map<String, String> hashMap = JSONObject.parseObject(msg, HashMap.class);

            String orderId = hashMap.get("orderId");

            String oneMinuteKey = "order_auth_isSend_" + orderId;
            Object o = redisTemplate.opsForValue().get(oneMinuteKey);
            if (Objects.isNull(o)) {
                smsVariableSend.sendLink(hashMap);
                redisTemplate.opsForValue().set(oneMinuteKey,"1",1, TimeUnit.MINUTES);
            }




        }

        @RabbitListener(queues = "sendYZM")
        public void sendYZM(String msg) {


            SysOrderOne orderOne = JSONObject.parseObject(msg, SysOrderOne.class);
            Long orderId = orderOne.getId();
            String phone = orderOne.getPhone();


            String oneMinuteKey = "order_empower_isSend_" + orderId;
            Object o = redisTemplate.opsForValue().get(oneMinuteKey);
            if (Objects.isNull(o)) {
                String yzm = smsSendDemo.sendYZM(orderId, phone);

                redisTemplate.opsForValue().set(oneMinuteKey, "1", 1, TimeUnit.MINUTES);

                //将验证码保存到redis中
                String key = "order_empower_" + orderId;

                redisTemplate.opsForValue().set(key, yzm, 5, TimeUnit.MINUTES);
            }


        }


        //发送发起背调的
    @RabbitListener(queues = "backtone")
    public void backtone(String msg) {

        SysOrderOne orderOne = JSONObject.parseObject(msg, SysOrderOne.class);
        HttpServletRequest request =null;

        HttpServletResponse response = null;
        surveyUtils.SurveyImp(orderOne, request, response);
    }





}
